﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace Labthink.PCSoft.UniversalHost4.Utility
{
    public class InputPanelHelper
    {
        private const Int32 WmSyscommand = 274;
        private const UInt32 ScClose = 61536;
        private const string ScreenKeyboardPath = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
        private static readonly RegistryKey RegistryKey = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\TabletTip\\1.7");
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool PostMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
        public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int RegisterWindowMessage(string lpString);
        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary>
        /// 设置虚拟键盘是否停靠
        /// </summary>
        /// <param name="isDocked"></param>
        public static void SetKeyboardDockedMode(bool isDocked)
        {
            RegistryKey.SetValue("EdgeTargetDockedState", Convert.ToInt32(isDocked), RegistryValueKind.DWord);
        }

        /// <summary>
        /// 设置键盘的布局
        /// </summary>
        /// <param name="mode"></param>
        public static void SetKeyboardLayoutMode(KeyboardLayoutMode mode)
        {
            if (mode == KeyboardLayoutMode.Handwriting)
            {
                RegistryKey.SetValue("KeyboardLayoutPreference", 0, RegistryValueKind.DWord);
                RegistryKey.SetValue("LastUsedModalityWasHandwriting", 1, RegistryValueKind.DWord);
            }
            else if (mode == KeyboardLayoutMode.ThumbLayout)
            {
                RegistryKey.SetValue("KeyboardLayoutPreference", 1, RegistryValueKind.DWord);
                RegistryKey.SetValue("LastUsedModalityWasHandwriting", 0, RegistryValueKind.DWord);
                // 0 = small, 1 = medium, 2 = large
                RegistryKey.SetValue("ThumbKeyboardSizePreference", 2, RegistryValueKind.DWord);
            }
            else
            {
                RegistryKey.SetValue("KeyboardLayoutPreference", 0, RegistryValueKind.DWord);
                RegistryKey.SetValue("LastUsedModalityWasHandwriting", 0, RegistryValueKind.DWord);
            }
        }

        public static void ShowInputPanel()
        {
            foreach (var process in Process.GetProcessesByName("TabTip"))
            {
                process.Kill();
            }

            Process.Start(ScreenKeyboardPath);
            
        }

        public static void ShowInputPanel(int x, int y)
        {
            ShowInputPanel();
            var touchhWnd = FindWindow("IPTip_Main_Window", null);
            while (IntPtr.Zero == touchhWnd)
            {
                System.Threading.Thread.Sleep(100);
                touchhWnd = FindWindow("IPTip_Main_Window", null);
            }
            MoveWindow(touchhWnd, x, y, 1000, 300, true);
            SetForegroundWindow(touchhWnd);
        }

        public static void HideInputPanel()
        {
            var touchhWnd = FindWindow("IPTip_Main_Window", null);
            if (touchhWnd == IntPtr.Zero)
                return;
            PostMessage(touchhWnd, WmSyscommand, ScClose, 0);
        }

    }

    public enum KeyboardLayoutMode
    {
        Default,
        ThumbLayout,
        Handwriting
    }
}
